package com.lksoft.bz;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class BeadRenderer extends Component implements ListCellRenderer<Bead> {
	private static final long serialVersionUID = -9219615285472264964L;
	
	private Bead bead;
	private boolean isSelected;
	
	public BeadRenderer(){
		setPreferredSize(new Dimension(40, 40));
	}

	@Override
	public Component getListCellRendererComponent(JList<? extends Bead> list,
			Bead bead, int index, boolean isSelected, boolean hasFocus) {
		this.bead = bead;
		this.isSelected = isSelected;
		return this;
	}

	@Override
	public void paint(Graphics g){
		Color back = isSelected ? 
						ColorMap.getColor(140<<16 | 190<<8 | 220) :
						ColorMap.getColor(245<<16 | 245<<8 | 250);
			
		g.setColor(back);
		g.fillRect(0, 0, getWidth(), getHeight());
		
		// Separators
		g.setColor(Color.CYAN);
		g.drawLine(0, 2, getWidth(), 2);
		g.drawLine(0, getHeight()-2, getWidth(), getHeight()-2);
		
		
		// Draw the color hex
		g.setColor(Color.BLACK);
		g.drawString(bead.getRGBString(), 40, 15);
		
		// Color name
		g.drawString(bead.getName(), 40, 32);
		
		((Graphics2D)g).scale(3.0f, 3.0f);
		// Draw the bead
		StitchPanel.drawBead(g, 2, 2, bead.getColor(), false);
	}
}
